package com.acce.service.card.service.impl;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.acce.service.card.dao.ILogDao;
import com.acce.service.card.exception.CommonException;
import com.acce.service.card.pojo.AccessLog;
import com.acce.service.card.pojo.AccessLoginLog;
import com.acce.service.card.pojo.AccessOperLog;
import com.acce.service.card.pojo.ArgsPojo;
import com.acce.service.card.service.ILogService;
import com.acce.service.utils.Utils;

@Service("logService")
public class LogServiceImpl implements ILogService {
	
	@Resource	
	private ILogDao logDao;
	
	@Override
	public boolean insertLog(ArgsPojo args) throws CommonException{
		// TODO Auto-generated method stub
		AccessLog log = new AccessLog();
		log.setServiceCode(args.getServiceCode());
		log.setAccessUser(args.getEditUser());
		log.setAccessTime(Utils.getCurrTime());
		log.setAccessDevice(args.getDevice());
		log.setAccessIp(args.getIpaddr());
		log.setAccessMac(args.getMac());
		AccessLog lastLog = getLastLog(args);
		if(lastLog != null){
			log.setLastAccessId(lastLog.getLogId());
			log.setLastAccessTime(lastLog.getAccessTime());
		}
		return logDao.insertLog(log) > 0 ? true : false;
	}

	@Override
	public AccessLog getLastLog(ArgsPojo args) {
		AccessLog log = new AccessLog();
		log.setAccessIp(args.getIpaddr());
		log.setAccessMac(args.getMac());
		log.setServiceCode(args.getServiceCode());
		return logDao.getLastLog(log);
	}

	@Override
	public AccessLog getLogById(String logId) {
		return logDao.getLogById(logId);
	}

	@Override
	public boolean countByTime(ArgsPojo args, int time, String currTime) throws ParseException,CommonException {
		Map<String, String> map = new HashMap();
		String st = Utils.timeChange(currTime, "s", "m", 3);
		map.put("startTime", st);
		map.put("endTime", currTime);
		System.out.println("st:" + st + " end:" + currTime);
		map.put("accessIp", args.getIpaddr());
		map.put("accessMac", args.getMac());
		return logDao.countByTime(map) >= 5 ? true: false;
	}

	@Override
	public boolean insertOperLog(ArgsPojo args) throws CommonException{
		AccessOperLog aol = new AccessOperLog();
		aol.setOperId(Utils.getUUID());
		aol.setServiceCode(args.getServiceCode());
		aol.setOperType(args.getOperType());
		aol.setOperContent(args.getOperContent());
		aol.setOperResult(args.getOperResult());
		aol.setOperServer(args.getOperServer());
		aol.setOperUser(args.getEditUser());
		aol.setOperTime(Utils.getCurrTime());
		aol.setOperIp(args.getIpaddr());
		aol.setOperDevice(args.getDevice());
		aol.setOperMac(args.getMac());
		return logDao.insertOperLog(aol) > 0 ? true : false;
	}

	@Override
	public boolean insertLoginLog(ArgsPojo args) {
		AccessLoginLog all = new AccessLoginLog();
		all.setLoginLogId(Utils.getUUID());
		all.setAtUserId(args.getAtUserId());
		all.setLoginLogType(args.getLoginLogType());
		all.setLoginLogContent(args.getLoginLogContent());
		all.setLoginLogTime(Utils.getCurrTime());
		all.setLoginLogDevice(args.getDevice());
		all.setLoginLogIp(args.getIpaddr());
		all.setLoginLogMac(args.getMac());
		return logDao.insertLoginLog(all) > 0 ? true : false;
	}
}
